package com.solcoo.customer.util;

import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.solcoo.customer.R;
import com.zxing.activity.CaptureActivity;

public class ViewUtil {

	public static final String TAG = "ViewUtil";

	private static final HashMap<String, Integer> GAP_MAP = new HashMap<String, Integer>();

	public static void setGoBackClickListener(View v) {
		if (v == null) {
			return;
		}
		v.setVisibility(View.VISIBLE);
		v.setOnClickListener(new OnGoBackClickListener());
	}

	private static class OnGoBackClickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			((Activity) v.getContext()).finish();
		}
	};

	public static void startCapture(Context context, int requestCode) {
		Intent it = new Intent(context, CaptureActivity.class);
		it.putExtra(CaptureActivity.INTENT_OF_TITLE_VIEW, R.layout.title);
		((Activity) context).startActivityForResult(it, requestCode);
	}

	/**
	 * 设置当数据为空时显示页面
	 * 
	 * @param mLinearNoData
	 * @param activity
	 * @param exceptIds
	 */
	public static void showNotData(View mLinearNoData, Activity activity) {

		if (mLinearNoData == null) {
			Log.w(TAG, activity + " mLinearNoData view is null!!!");
			return;
		}
		if (activity == null) {
			Log.w(TAG, "activity is null!!!");
			return;
		}
		final int marginTop = getViewHeight(activity
				.findViewById(R.id.title_view));

		final int marginBottom = getViewHeight(activity
				.findViewById(R.id.toolbar_view));

		FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
				FrameLayout.LayoutParams.MATCH_PARENT,
				FrameLayout.LayoutParams.MATCH_PARENT);
		params.gravity = Gravity.FILL;
		params.topMargin = marginTop;
		params.bottomMargin = marginBottom;
		if (mLinearNoData.getParent() == null) {
			activity.addContentView(mLinearNoData, params);
		}
		TextView wordView = (TextView) mLinearNoData
				.findViewById(R.id.no_data_desc);
		wordView.setText("亲，木有数据哦！");
		mLinearNoData.findViewById(R.id.retry).setVisibility(View.GONE);
		mLinearNoData.setVisibility(View.VISIBLE);
	}

	private static int getViewHeight(View v) {
		if (v == null) {
			return 0;
		}
		int height = v.getHeight();
		if (height > 0) {
			return height;
		}

		final String key = v.getId() + "_height";

		height = GAP_MAP.get(key);
		if (height > 0) {
			return height;
		}

		v.measure(View.MeasureSpec.makeMeasureSpec(0,
				View.MeasureSpec.UNSPECIFIED), View.MeasureSpec
				.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
		height = v.getMeasuredHeight();
		GAP_MAP.put(key, height);
		return height;
	}

}
